package com.canyou.util;

import com.jfinal.plugin.activerecord.Page;

public class PageHelper {
	public static int DEFAULT_PAGE_SIZE = 10;

	private int total;// 总共的数据量

	private int pageSize;// 每页显示多少条

	private int totalPage;// 共有多少页

	private int index;// 当前是第几页

	private String path = "";// 连接路径

	private int startPage; // 开始页面

	private int endPage; // 结束页面

	private int displayNum = 10; // 显示的页数

	public PageHelper(Page<?> page, String path) {
		total = page.getTotalRow();
		pageSize = page.getPageSize();
		totalPage = page.getTotalPage();
		index = page.getPageNumber();
		this.path = path;
	}

	/**
	 * 设置路径前缀，页面第一页index为1
	 * 
	 * @param path
	 *            此path含有参数形式，如/aa/article?page=,或者/bb/article/list/
	 */
	public void setPath(String path) {
		this.path = path;
	}

	public int getIndex() {
		return index;
	}

	public int getPageSize() {
		return pageSize;
	}

	public String getPageHTML() {
		StringBuffer displayInfo = new StringBuffer();
		if (totalPage != 0 && pageSize != 0) {
			displayInfo
					.append("<div id='anpListPager' class='paginator' style='height:20px;width:99%;text-align:right;'>");
			displayInfo
					.append("<table width='100%' border='0' cellpadding='0' cellspacing='0' style='height:20px;'>");
			displayInfo
					.append("<tr><td class='customInfo' align='left' valign='bottom' nowrap='true' style='height:20px;width:40%;'>");
			displayInfo.append("第" + index + "页，共" + totalPage + "页，每页"
					+ pageSize + "条，共" + total + "条记录");
			displayInfo
					.append("</td><td align='right' class='paginator' valign='bottom' nowrap='true' style='width:60%;'>");
			// 判断如果当前是第一页 则“首页”和“第一页”失去链接
			if (index <= 1) {
				displayInfo
						.append("<a class='link_class' disabled='disabled' style='margin-right:5px;'>首页</a>");
				displayInfo
						.append("<a class='link_class' disabled='disabled' style='margin-right:5px;'>上一页</a>");
			} else {
				displayInfo.append("<a class='link_class' href='" + path + 1
						+ "' style='margin-right:5px;'>首页</a>");
				displayInfo.append("<a class='link_class' href='" + path
						+ (index - 1) + "' style='margin-right:5px;'>上一页</a>");
			}

			countPages();
			for (int i = startPage; i <= endPage; i++) {
				if (i == index) {
					displayInfo
							.append("<a class='link_acted' style='margin-right:5px;'>"
									+ i + "</a>");
				} else {
					displayInfo.append("<a href='" + path + i
							+ "' class='list_ye' style='margin-right:5px;'>"
							+ i + "</a>");
				}
			}

			if (index >= totalPage) {
				displayInfo
						.append("<a class='link_class' disabled='disabled' style='margin-right:5px;'>下一页</a>");
				displayInfo
						.append("<a class='link_class' disabled='disabled' style='margin-right:5px;'>末页</a>");
			} else {
				displayInfo.append("<a class='link_class' href='" + path
						+ (index + 1) + "' style='margin-right:5px;'>下一页</a>");
				displayInfo.append("<a class='link_class' href='" + path
						+ totalPage + "' style='margin-right:5px;'>末页</a>");
			}
			displayInfo.append("</td></tr></table></div>");
		}
		return displayInfo.toString();
	}

	/**
	 * 计算起始页和结束页
	 */
	public void countPages() {
		if (index - displayNum / 2 < 1) {
			startPage = 1;
			endPage = displayNum > totalPage ? totalPage : displayNum;
		} else if (index + displayNum / 2 > totalPage) {
			int n = totalPage - displayNum + 1;
			startPage = n > 0 ? n : 1;
			endPage = totalPage;
		} else {
			startPage = index - displayNum / 2;
			endPage = startPage + displayNum - 1;
		}
	}
}